# -*- coding: utf-8 -*-
# this file is released under public domain and you can use without limitations

#########################################################################
## This scaffolding model makes your app work on Google App Engine too
#########################################################################

if request.env.web2py_runtime_gae:
    from gluon.contrib.gql import *
    # connect to Google BigTable
    db = DAL('gae')
    # and store sessions there
    session.connect(request, response, db=db)
else:
    # if not, use SQLite or other DB
    db = DAL('sqlite://storage.sqlite')      # if not, use SQLite or other DB

# by default give a view/generic.extension to all actions from localhost
# none otherwise. a pattern can be 'controller/function.extension'
#response.generic_patterns = ['*'] if request.is_local else []
#########################################################################
## Here is sample code if you need for
## - email capabilities
## - authentication (registration, login, logout, ... )
## - authorization (role based authorization)
## - services (xml, csv, json, xmlrpc, jsonrpc, amf, rss)
## - crud actions
## (more options discussed in gluon/tools.py)
#########################################################################

from gluon.tools import Auth, Crud, Service, PluginManager, prettydate


auth = Auth(db)                                # authentication/authorization
crud = Crud(db)                                # for CRUD helpers using auth
service = Service()                            # for json, xml, jsonrpc, xmlrpc, amfrpc
plugins = PluginManager()                      # for configuring plugins



auth.settings.hmac_key = 'sha512:fdfa5a0c-27d5-45da-a688-e5af94970c67'   # before define_tables()
auth.define_tables()                           # creates all needed tables
auth.settings.registration_requires_verification = False
auth.settings.registration_requires_approval = False
auth.messages.verify_email = 'Click on the link http://'+request.env.http_host+URL('default','user',args=['verify_email'])+'/%(key)s to verify your email'
auth.settings.reset_password_requires_verification = True
auth.messages.reset_password = 'Click on the link http://'+request.env.http_host+URL('default','user',args=['reset_password'])+'/%(key)s to reset your password'

#########################################################################
## If you need to use OpenID, Facebook, MySpace, Twitter, Linkedin, etc.
## register with janrain.com, uncomment and customize following
# from gluon.contrib.login_methods.rpx_account import RPXAccount
# auth.settings.actions_disabled = \
#    ['register','change_password','request_reset_password']
# auth.settings.login_form = RPXAccount(request, api_key='...',domain='...',
#    url = "http://localhost:8000/%s/default/user/login" % request.application)
## other login methods are in gluon/contrib/login_methods
#########################################################################

crud.settings.auth = None        # =auth to enforce authorization on crud

#########################################################################
## Define your tables below (or better in another model file) for example
##
## >>> db.define_table('mytable',Field('myfield','string'))
##
## Fields can be 'string','text','password','integer','double','boolean'
##       'date','time','datetime','blob','upload', 'reference TABLENAME'
## There is an implicit 'id integer autoincrement' field
## Consult manual for more options, validators, etc.
##
## More API examples for controllers:
##
## >>> db.mytable.insert(myfield='value')
## >>> rows=db(db.mytable.myfield=='value').select(db.mytable.ALL)
## >>> for row in rows: print row.id, row.myfield
#########################################################################
"""
just copy paste this code into your model and replace dbOBJECT to something
you prefer or what is used in your code, another thing that should be known is that
this code still cannot distinguish what exactly should be in reference title - name
when you use generic appadmin so please remove all fiealds you don't need not require 
"""
"""
database class object creation
"""


"""
Table definition
"""
db.define_table("item",
      SQLField("name", "text", length=512, notnull=True, default=None),
      SQLField("base_price", "integer", notnull=True, default=None),
      SQLField("price", "integer", notnull=True, default=None)
      )


"""
Table definition
"""
from datetime import date
import time

db.define_table("sale",
      SQLField("id_items", db.item),
      SQLField("date", "date", notnull=True, default=date.today()),
      SQLField("sale_time", "datetime", notnull=True, default=request.now),
      SQLField("total_items", "integer", notnull=True, default=None),
      SQLField("description", "text", notnull=False, default=None))


#db.define_table("sale_archive",
      #SQLField("item_name", "text"),
      #SQLField("date", "date", notnull=True, default=None),
      #SQLField("price", "integer", notnull=True, default=None),
      #SQLField("total_items", "integer", notnull=True, default=None),
      #SQLField("total_price", compute= lambda r:r['price'] * r ['total_items']),
      #SQLField("description", "text", notnull=False, default=None))

#db.sale.drop()
"""
Relations between tables (remove fields you don't need from requires)
"""
db.sale.id_items.requires=IS_IN_DB(db, 'item.id')
